package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.response.PageResult;
import com.itheima.response.Result;
import com.itheima.response.constant.MessageConstant;
import com.itheima.resquest.QueryPageBean;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public Result<List<CheckItem>> findAll() {
        List<CheckItem> checkItems = checkItemMapper.findAll();
        return new Result<>(checkItems);
    }

    @Override
    public Result search(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage==null){
            currentPage=1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize==null){
            pageSize=10;
        }
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> checkItemPage= (Page<CheckItem>) checkItemMapper.search(queryString);
        if (checkItemPage==null || checkItemPage.getTotal()==0){
            return new Result("查询失败");
        };
        PageResult<CheckItem> pageResult=new PageResult(checkItemPage.getTotal(),checkItemPage.getResult());
        return new Result(pageResult);
    }

    @Override
    public Result<CheckItem> findOne(Integer id) {
        CheckItem one = checkItemMapper.findOne(id);
        if (one==null){
            return new Result("无当前检查项可以修改");
        }
        return new Result<>(one);
    }

    @Override
    public Result<CheckItem> edit(Integer id, CheckItem checkItem) {
        CheckItem one = checkItemMapper.findOne(id);
        if (one==null){
            return new Result("无当前检查项可以修改");
        }
        checkItem.setId(id);
        checkItemMapper.edit(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }

    @Override
    public Result<CheckItem> add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
        return new Result(checkItem);
    }

    @Override
    public Result delete(Integer id) {
        checkItemMapper.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
    }
}
